package com.dikeleakagedetection.myapplication.ui.dataMonitor

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.CheckBox
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.dikeleakagedetection.myapplication.R
import com.dikeleakagedetection.myapplication.logic.model.TabletSimple

class TabletSimpleAdapter(context: SelectTabletDialogFragment, private val tabletSimpleList: List<TabletSimple>):RecyclerView.Adapter<TabletSimpleAdapter.ViewHolder>() {
    lateinit var onItemClick: (Int) -> Unit

    class ViewHolder(view: View):RecyclerView.ViewHolder(view) {
        val cbTabletId = view.findViewById<CheckBox>(R.id.cb_tablet_simple)
        val tvTabletPlace = view.findViewById<TextView>(R.id.tv_tablet_simple_place)
        val tvTabletDate = view.findViewById<TextView>(R.id.tv_tablet_simple_date)
        val tvTabletDikeLength = view.findViewById<TextView>(R.id.tv_tablet_simple_length)
        val tvTabletDikeWidth = view.findViewById<TextView>(R.id.tv_tablet_simple_width)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_tablet_simple_dlg_select_tablet, parent, false)
        val viewHolder = ViewHolder(view)
        return viewHolder
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val tabletSimple = tabletSimpleList[position]
        holder.cbTabletId.text = tabletSimple.id.toString()
        holder.cbTabletId.isChecked = tabletSimple.isChecked
        holder.tvTabletPlace.text = tabletSimple.place
        holder.tvTabletDate.text = tabletSimple.date
        holder.tvTabletDikeLength.text = tabletSimple.length.toString()
        holder.tvTabletDikeWidth.text = tabletSimple.width.toString()

        holder.cbTabletId.setOnClickListener {
            for(i in tabletSimpleList.indices){
                tabletSimpleList[i].isChecked = i == position
            }
            onItemClick.invoke(position)
        }
    }

    override fun getItemCount() = tabletSimpleList.size

    fun setOnItemClickListener(onItemClick: (Int) -> Unit) {
        this.onItemClick= onItemClick
    }



}